"""
Case Type   : DSS元数据修复工具
Case Name   :  dsstbox ssrepair参数au_size无效值验证
Create At   : 2025/04
Owner       : haomeng
Description :
    1、查看当前卷名
    2、开启dss的debug日志
    3、创建dss文件，并truncate一个大小
    4、查看dss日志文件，筛选出创建文件的metaid
    5、修改元数据指定不符合范围的au_size
    6、清理环境
Expect      :
    1、查看成功
    2、开启debug日志成功
    3、创建文件成功
    5、获取metaid成功
    5、修改失败
    6、清理环境成功
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DSSrepairtools(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node = Node('PrimaryDbUser')

        self.vol_path = os.path.join(macro.DSS_INSTANCE_PATH, 'cfg/dss_vg_conf.ini')
        self.dss_log_path = os.path.join(macro.DSS_INSTANCE_PATH, 'log/debug/dssinstance.dlog')
        self.file_name = 'dsstbox_ssrepair_file_0012.txt'
        self.metadata = ['common.type', 'common.version', 'common.id']
        self.metadata_value = [3707, 8192, 2]
        self.au_size = [2047, 65537]

    def test_dsstbox(self):
        text = '-----step1:查看当前卷名;  expect:查看成功-----'
        self.log.info(text)

        res = self.user_node.sh(f"cat {self.vol_path} | awk 'NR==1'").result()
        self.log.info(res)
        self.vol_name = res.split(':')[0]
        vol_value = res.split(':')[1]
        self.log.info(f'vol_name的值是：{self.vol_name}, vol_value的值是：{vol_value}')
        self.assertNotEqual('', self.vol_name, '查看当前卷名失败')

        text = '-----step2:开启debug日志;  expect:开启成功-----'
        self.log.info(text)

        set_debug = f"source {macro.DB_ENV_PATH};" \
                    f"dsscmd setcfg -n _log_LEVEL -v 255 -s both"
        self.log.info(set_debug)
        execute_cmd = self.user_node.sh(set_debug).result()
        self.log.info(execute_cmd)
        self.assertIn('Succeed to set cfg', execute_cmd, '开启debug日志失败')

        text = '-----step3:创建dss文件，并truncate一个大小;  expect:创建成功-----'
        self.log.info(text)

        touch = f"source {macro.DB_ENV_PATH};" \
                f"dsscmd touch -p +{self.vol_name}/{self.file_name}"
        self.log.info(touch)
        execute_touch = self.user_node.sh(touch).result()
        self.log.info(execute_touch)
        self.assertIn('Succeed to create file', execute_touch, '创建文件失败')

        truncate = f"source {macro.DB_ENV_PATH};" \
                   f"dsscmd truncate -p +{self.vol_name}/{self.file_name} -l 8192"
        self.log.info(truncate)
        execute_truncate = self.user_node.sh(truncate).result()
        self.log.info(execute_truncate)
        self.assertIn('Success to truncate file', execute_truncate, 'truncate文件失败')

        text = '-----step4:查看dss日志文件，筛选出创建文件的metaid;  expect:查看成功-----'
        self.log.info(text)

        grep_cmd = f"cat {self.dss_log_path} | grep {self.file_name} | grep metaid | tail -n 1"
        self.log.info(grep_cmd)
        grep_res = self.user_node.sh(grep_cmd).result()
        self.log.info(grep_res)

        metaid = grep_res.split('metaid')[1].split(':')[1].split(' ')[0].strip()
        self.log.info(f'metaid的值是：{metaid}')
        self.assertNotEqual('', metaid, '查看metaid失败')

        text = '-----step5:修改元数据指定不符合范围的au_size;  expect:修改失败-----'
        self.log.info(text)

        for param in self.metadata:
            for size in self.au_size:
                cmd1 = f"dsstbox ssrepair -v {vol_value} -t ft_block -i {metaid} -s {size} -k '{param}={self.metadata_value[self.metadata.index(param)]}'"
                self.log.info(cmd1)
                execute_cmd1 = f'''source {macro.DB_ENV_PATH};yes|{cmd1}'''
                self.log.info(execute_cmd1)
                res1 = self.user_node.sh(execute_cmd1).result()
                self.log.info(res1)
                self.assertIn(f'au_size {size} is error', res1, 'au_size的值符合范围')
                self.assertIn('ssrepair execute failed', res1, '修改元数据成功')

    def tearDown(self):
        text = '-----step6:清理环境;  expect:清理成功-----'
        self.log.info('text')

        del_cmd = f"source {macro.DB_ENV_PATH};dsscmd rm -p +{self.vol_name}/{self.file_name}"
        self.log.info(del_cmd)
        execute_del = self.user_node.sh(del_cmd).result()
        self.log.info(execute_del)
        self.assertIn(f'Succeed to remove device +{self.vol_name}/{self.file_name}', execute_del, '删除文件失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
